﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace House_World
{
    public class Billboard : Transform
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private Matrix world, view, projection;
        private VertexPositionTexture[] verts;
        private VertexBuffer vertexBuffer;
        private Texture2D texture;
        private BasicEffect effect;
        public double module;

        public Billboard(GraphicsDevice graphicsDevice, GameWindow window, ContentManager content,int x, float z)
        {
            verts = new VertexPositionTexture[]{
            new VertexPositionTexture(new Vector3(-1f, 1f, 0), new Vector2(0, 0)),
            new VertexPositionTexture(new Vector3(1f, 1f, 0), new Vector2(1, 0)),
            new VertexPositionTexture(new Vector3(-1f, -1f, 0), new Vector2(0, 1)),
            new VertexPositionTexture(new Vector3(1f, -1f, 0), new Vector2(1, 1))

        };
            position = new Vector3(x*7, 1f, -z*7);
            vertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionTexture),
            verts.Length, BufferUsage.None);
            vertexBuffer.SetData<VertexPositionTexture>(verts);
            world = Matrix.Identity;
            view = Matrix.CreateLookAt(new Vector3(0, 0, 5), Vector3.Zero, Vector3.Up);
            projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, window.ClientBounds.Width /
            (float)window.ClientBounds.Height, 0.1f, 100);
            texture = content.Load<Texture2D>(@"Texture\tree");
            effect = new BasicEffect(graphicsDevice);
            world *= Matrix.CreateTranslation(new Vector3(0, 0.4f, -1));
        }
        public void captureModule(Vector3 originPosition)
        {
            double A = Math.Abs(originPosition.X - position.X);
            double B = Math.Abs(originPosition.Z - position.Z);
            double squareRoot = Math.Pow(A, 2) + Math.Pow(B, 2);
            module = Math.Sqrt(squareRoot);
        }
        public void Draw(GraphicsDevice graphics,Camera camera)
        {
            world = Matrix.Invert(camera.View);
            world.Translation = position;
            effect.World = world;
            effect.View = camera.View;
            effect.Projection = camera.Projector;
            effect.Texture = texture;
            effect.TextureEnabled = true;
            graphics.RasterizerState = RasterizerState.CullNone;
            graphics.SamplerStates[0] = SamplerState.LinearClamp;
            graphics.BlendState = BlendState.AlphaBlend;
            captureModule(camera.positions[0]);
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                try
                {
                    graphics.DrawUserPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, verts, 0, 2);
                }
                catch (NotSupportedException)
                {
                    graphics.SamplerStates[0] = SamplerState.LinearClamp;
                }
            }
            graphics.BlendState = BlendState.Opaque;
        }
    }

}
